package com.jms.fu.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.common.UIMessage;
import com.jms.fu.exception.DAOException;
import com.jms.fu.form.UserForm;
import com.jms.fu.model.bean.User;
import com.jms.fu.model.bo.UserBO;

public class GoDevAction extends Action {
	private static Logger logger = Logger.getLogger(LoginAction.class);

	/**
	 * Method execute, called when executing the action
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form;
		UserBO userBO = new UserBO();
		HttpSession session = request.getSession();
			String bankAccountNo = userForm.getBankAccountNo();
		String bankAccountName = userForm.getBankAccountName();
		String bankName = userForm.getBankName();
		String paypal = userForm.getPaypal();
		String username = (String) session.getAttribute("username");
		User user = (User) session.getAttribute("userInfo");
		User userInfo = new User();
		float balance = user.getBalance();
	
		try {
			if (balance < 200) {
				request.setAttribute("info", UIMessage.INSUFFICIENT_FUNDS );
				return mapping.findForward("failed");
			}
			
			
			userBO.goDev(username,bankAccountNo,bankAccountName,bankName,balance,paypal);
			userInfo = userBO.getUserInfoByUsername(username);
			session.setAttribute("userInfo", userInfo);
			request.setAttribute("info", UIMessage.GO_DEV_SUCCESSFULLY );
			session.setAttribute("role", "Developer");
			logger.info("Account: "+username+" go Dev success" );
			return mapping.findForward("success");
				
		} catch (DAOException e) {
			logger.error(e.getMessage());
			return mapping.findForward("error");
		}
		
	}
}
